package org.dbstar.scheduler.control.db.conf;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;

import org.dbstar.scheduler.beans.CmdJobConf;
import org.dbstar.scheduler.beans.CompoundJobConf;
import org.dbstar.scheduler.beans.JobConf;
import org.dbstar.scheduler.beans.LoopJobConf;
import org.dbstar.scheduler.beans.ParameterDefine;
import org.dbstar.scheduler.control.db.ObjectRowMapper;
import org.dbstar.scheduler.identifier.DefaultIdentifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.object.SqlQuery;


/**
 * JobConf的Query对象
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-24
 */
public class JobConfQuery extends SqlQuery<JobConf> {
	private static final JobConfRowMapper jobConfRowMapper = new JobConfRowMapper();

	public JobConfQuery(JdbcTemplate jdbcTemplate) {
		this(jdbcTemplate, null);
	}

	public JobConfQuery(JdbcTemplate jdbcTemplate, String condition) {
		super();
		setJdbcTemplate(jdbcTemplate);
		setSql("SELECT * FROM SCHD_JOB_CONF" + (condition == null ? "" : (" " + condition)));
	}

	@Override
	protected final RowMapper<JobConf> newRowMapper(Object[] parameters, @SuppressWarnings("rawtypes") Map context) {
		return jobConfRowMapper;
	}

	private static class JobConfRowMapper extends ObjectRowMapper<JobConf> {
		@Override
		public JobConf mapRow(ResultSet rs, int rowNum) throws SQLException {
			JobConf conf = buildJobConf(rs, rs.getString("TYPE"));
			conf.setIdentifier(DefaultIdentifier.valueOf(rs.getString("IDENTIFIER")));
			conf.setCatalog(rs.getInt("CATALOG"));
			conf.setName(rs.getString("NAME"));
			conf.setDescription(rs.getString("DESCRIPTION"));
			conf.setResource(rs.getString("RESOURCE"));
			conf.setContent(rs.getString("CONTENT"));
			conf.setParameters(readParameters(rs.getString("PARAMETER_DATA")));
			conf.setMaxRetry(rs.getInt("MAX_RETRY"));
			conf.setCreateTime(rs.getLong("CREATE_TIME"));
			conf.setModifyTime(getLongObject(rs, "MODIFY_TIME"));
			conf.setOwner(rs.getString("OWNER"));
			conf.setModifier(rs.getString("MODIFIER"));
			conf.setAverageRunTime(rs.getInt("AVERAGE_RUN_TIME"));
			return conf;
		}

		@SuppressWarnings("unchecked")
		private List<ParameterDefine> readParameters(String str) throws SQLException {
			if (str == null) {
				return null;
			} else {
				JSONArray array = JSONArray.fromObject(str);
				return (List<ParameterDefine>) JSONSerializer.toJava(array, new ParameterDefineJsonConfig());
			}
		}

		private JobConf buildJobConf(ResultSet rs, String type) throws SQLException {
			if (CmdJobConf.JOB_TYPE.equals(type)) {
				return new CmdJobConf();
			} else if (CompoundJobConf.JOB_TYPE.equals(type)) {
				return new CompoundJobConf();
			} else if (LoopJobConf.JOB_TYPE.equals(type)) {
				return new LoopJobConf();
			} else {
				throw new SQLException("Unknown job configuration type: " + type);
			}
		}
	}
}
